package com.brayan.webapp.controller.general.i18n;

import java.io.Serializable;
import java.util.List;

import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

import com.brayan.webapp.model.general.country.Country;
import com.brayan.webapp.model.general.i18n.Language;
import com.brayan.webapp.model.general.i18n.Locale;
import com.brayan.webapp.repository.general.country.CountryRepository;
import com.brayan.webapp.repository.general.i18n.LanguageRepository;
import com.brayan.webapp.repository.general.i18n.LocaleRepository;
import com.brayan.webapp.service.general.i18n.LocaleBean;

@Named
@SessionScoped
public class LocaleController implements Serializable {

	private static final long serialVersionUID = 2918681422742628612L;

	@Inject
	LocaleRepository localeRepo;
	@Inject
	LocaleBean localeService;
	@Inject
	CountryRepository countryRepo;
	@Inject
	LanguageRepository languageRepo;

	private Long id;
	private String localeCode;
	private String languageCode;
	private Language language;
	private String countryCode;
	private Country country;
	private String nativeLanguageName;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getLocaleCode() {
		return localeCode;
	}

	public void setLocaleCode(String localeCode) {
		this.localeCode = localeCode;
	}

	public String getLanguageCode() {
		return languageCode;
	}

	public void setLanguageCode(String languageCode) {
		this.languageCode = languageCode;
	}

	public String getCountryCode() {
		return countryCode;
	}

	public Language getLanguage() {
		return language;
	}

	public void setLanguage(Language language) {
		this.language = language;
	}

	public Country getCountry() {
		return country;
	}

	public void setCountry(Country country) {
		this.country = country;
	}

	public void setCountryCode(String countryCode) {
		this.countryCode = countryCode;
	}

	public String getNativeLanguageName() {
		return nativeLanguageName;
	}

	public void setNativeLanguageName(String nativeLanguageName) {
		this.nativeLanguageName = nativeLanguageName;
	}

	public List<Locale> getAllLocalesExcept(String localeCode){
		List<Locale> listOfLocales = localeService.getAllLocalesExcept(localeCode);
		return listOfLocales;
	}
	
	public List<Locale> getAllLocales() {
		return localeRepo.getAllLocales();
	}

	public void loadLocaleDetail() {
		HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
				.getRequest();
		String id = req.getParameter("id");
		Locale locale = localeRepo.getLocaleById(Long.parseLong(id));
		if (locale.getCountry() != null && !locale.getCountry().getCountryCode().isEmpty()) {
			setCountryCode(locale.getCountry().getCountryCode());
		}
		setLanguageCode(locale.getLanguage().getLanguageCode());
		setNativeLanguageName(locale.getLanguage().getNativeLanguageName());
	}

	/**
	 * Use method to save existing and new text units.
	 */
	public void saveLocale() {
		HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
				.getRequest();
		String languageCode = req.getParameter("localeUnitForm:languageCode");
		String countryCode = req.getParameter("localeUnitForm:countryCode");
		String nativeLanguageName = req.getParameter("localeUnitForm:nativeLanguageName");

		localeService.saveLocale(languageCode, countryCode, nativeLanguageName);
	}

	public void deleteLocale() {
		HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
				.getRequest();
		String id = req.getParameter("id");
		localeRepo.deleteLocale(Long.parseLong(id));
	}

}
